package com.ht.baselibs.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.telephony.TelephonyManager;

import com.ht.baselibs.application.HTApplication;

import java.io.File;
import java.util.UUID;

public class DeviceUtils {
    // 获取手机型号
    public static String getMobileType() {
        return Build.MODEL.replaceAll(" ", "");
    }

    public static String getSystemVersion() {
        return Build.VERSION.RELEASE;
    }

    public static String getDeviceId() {
        try {
            TelephonyManager tm = (TelephonyManager) HTApplication.getContext().getSystemService(Context.TELEPHONY_SERVICE);
            return tm.getDeviceId();
        } catch (Exception e) {
            e.printStackTrace();
            return UUID.randomUUID().toString();
        }
    }

    public static String getDeviceName() {
        return Build.BRAND + " " + Build.MODEL;
    }

    public static String getOsVersion() {
        return Build.VERSION.RELEASE;
    }

    public static String getAppVersionName() {
        String versionName = "";
        try {
            PackageManager packageManager = HTApplication.getContext().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(HTApplication.getContext().getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }

    public static int getAppVersionCode() {
        int versionCode = -1;
        try {
            PackageManager packageManager = HTApplication.getContext().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(HTApplication.getContext().getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    public static void installApk(Activity activity, String path) {
        File file = new File(path);
        Intent intent = new Intent(Intent.ACTION_VIEW);

        Uri data;
        // 判断版本大于等于7.0
        if (Build.VERSION.SDK_INT >= 24) {
            // "net.csdn.blog.ruancoder.fileprovider"即是在清单文件中配置的authorities
            data = FileProvider.getUriForFile(activity, "com.jinkaimen.addwallet.fileprovider", file);
            // 给目标应用一个临时授权
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            data = Uri.fromFile(file);
        }
        // intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        intent.setDataAndType(data, "application/vnd.android.package-archive");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(intent);
    }

    public static int getStatusBarHeight() {
        int result = 0;
        int resourceId = HTApplication.getContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = HTApplication.getContext().getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public static boolean isSDCardAvailable() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

}
